#include "GamePlay.h"
void GamePlay::GameInit(HINSTANCE p_instance,int nCmdshow)
{
	obj_register=new Register(p_instance,sm_handle);
	if (!obj_register->InitWindow(p_instance,nCmdshow))
	{
		return;
	}
	sm_handle=obj_register->GetHandle();
	obj_graphic=new Graphic;
	if (!obj_graphic->InitDevice(sm_handle))
	{
		return;
	}
	sm_device=obj_graphic->GetDevice();
	sm_backbuffer=obj_graphic->GetBackBuffer();
	sm_surface=obj_graphic->GetSurface();
}
void GamePlay::GameRun()
{
	MSG msg;
	int done = 0;
	DWORD frame_start = GetTickCount();;
	DWORD tick_per_frame = 1000 / FRAME_RATE;
	while (!done) 
	{
		if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{
			if (msg.message==WM_QUIT) done=1;

			TranslateMessage(&msg);
			DispatchMessage(&msg);			
		}

		DWORD now = GetTickCount();
		if (now - frame_start >= tick_per_frame) 
		{
			frame_start = now;
			Render();
		}
	}
}
void GamePlay::Render()
{
	if (sm_device->BeginScene()) 
	{
		// Clear back buffer with BLACK
		sm_device->ColorFill(sm_backbuffer,NULL,D3DCOLOR_XRGB(0,0,0));

		// Random color for the surface
		int r = rand() % 255; 
		int g = rand() % 255; 
		int b = rand() % 255;

		// Fill the bitmap
		sm_device->ColorFill(sm_surface,NULL,D3DCOLOR_XRGB(r,g,b));

		// Generate a random area (within back buffer) to draw the surface onto
		RECT rect; 
		rect.left = rand() % (SCREEN_WIDTH/2);
		rect.top = rand() % (SCREEN_HEIGHT/2);
		rect.right = rect.left + rand() % (SCREEN_WIDTH/2);
		rect.bottom = rect.left + rand() % (SCREEN_HEIGHT/2);

		// Draw the surface onto the back buffer
		sm_device->StretchRect(
			sm_surface,			// from 
			NULL,				// which portion?
			sm_backbuffer,		// to 
			&rect,				// which portion?
			D3DTEXF_NONE);
		sm_device->EndScene();
	}
		sm_device->Present(NULL,NULL,NULL,NULL);
}
void GamePlay::GameEnd()
{
	obj_graphic->Release();
	delete obj_graphic;
	delete obj_register;
}